package com.ruoyi.robot.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.robot.enums.OrderStatus;
import com.ruoyi.robot.pojo.po.OrderInfo;
import com.ruoyi.robot.service.OrderInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.core.domain.AjaxResult.success;
import static com.ruoyi.common.utils.PageUtils.startPage;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yid
 * @since 2023-01-16
 */
@RestController
@RequestMapping("/robot/orderInfo")
@Api(tags = "订单接口")
public class OrderInfoController extends BaseController {

    @Resource
    private OrderInfoService orderInfoService;

    @ApiOperation("获取所有订单列表")
    @GetMapping("/list1")
    public AjaxResult list() {
        List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDesc();
        return AjaxResult.success("list", list);
    }

    @ApiOperation("获取单个用户订单列表")
    @GetMapping("/listByUserId")
    public AjaxResult listByUserId(@RequestParam Long userId) {
        if (userId == null) {
            return AjaxResult.error("参数不全");
        }
        List<OrderInfo> list = orderInfoService.listOrderByCreateTimeDesc(userId);
        return AjaxResult.success("list", list);
    }

    /**
     * 查询本地订单状态
     *
     * @param orderNo
     * @return
     */
    @ApiOperation("查询本地订单状态")
    @GetMapping("/query-order-status/{orderNo}")
    public AjaxResult queryOrderStatus(@PathVariable @ApiParam("订单号") String orderNo) {

        String orderStatus = orderInfoService.getOrderStatus(orderNo);
        if (OrderStatus.SUCCESS.getType().equals(orderStatus)) {
            //支付成功
            return success("支付成功");
        }
        return new AjaxResult(HttpStatus.PAYING, "支付中");
    }

    /**
     * 查询订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('orderInfo:orderInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderInfo orderInfo) {
        startPage();
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        return getDataTable(list);
    }

    /**
     * 导出订单管理列表
     */
    @PreAuthorize("@ss.hasPermi('orderInfo:orderInfo:export')")
    @Log(title = "订单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderInfo orderInfo) {
        List<OrderInfo> list = orderInfoService.selectOrderInfoList(orderInfo);
        ExcelUtil<OrderInfo> util = new ExcelUtil<OrderInfo>(OrderInfo.class);
        util.exportExcel(response, list, "订单管理数据");
    }

    /**
     * 获取订单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('orderInfo:orderInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(orderInfoService.selectOrderInfoById(id));
    }

    /**
     * 新增订单管理
     */
    @PreAuthorize("@ss.hasPermi('orderInfo:orderInfo:add')")
    @Log(title = "订单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.insertOrderInfo(orderInfo));
    }

    /**
     * 修改订单管理
     */
    @PreAuthorize("@ss.hasPermi('orderInfo:orderInfo:edit')")
    @Log(title = "订单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderInfo orderInfo) {
        return toAjax(orderInfoService.updateOrderInfo(orderInfo));
    }

    /**
     * 删除订单管理
     */
    @PreAuthorize("@ss.hasPermi('orderInfo:orderInfo:remove')")
    @Log(title = "订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(orderInfoService.deleteOrderInfoByIds(ids));
    }

}

